package com.compty.paint.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.compty.core.framework.context.spring.SpringContextHolder;
import com.compty.paint.manager.SpecialSubjectManager;
import com.compty.paint.util.Constants;
import com.compty.security.domain.User;

/**
 * @Todo:广告
 * @author: zhuqh
 * @CreateTime:2011-9-15 下午05:56:35
 */
@Entity
@Table(name = "t_advertisement")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE,include="non-lazy")
public class Advertisement implements Serializable {

	private static final long serialVersionUID = -749888860203464896L;
	
	private Integer id;
	
	/************ 主题 ****************/
	private String title;
	
	/************ 链接地址 ****************/
	private String linkAddress;
	
	/************ 广告图片 ****************/
	private ImageInfo imageInfo;
	
	/************ 审核状态 ****************/
	private AuditState auditState;
	
	/************ 上传时间 ****************/
	private Date uploadTime;
	
	/************ 最后修改时间  ****************/
	private Date modifyTime;
	
	/************ 开始时间  ****************/
	private Date beginTime;
	
	/************ 结束时间  ****************/
	private Date endTime;
	
	/************ 录入人  ****************/
	private User entryPerson;
	
	/************ 修改人  ****************/
	private User modifyPerson;
	
	private String channelID;//所属频道(1:首页 2：瓷艺频道3:绘画4:艺术家具)
	
	private String channelName;//所属频道名称
	
	private String imagepath;//图片路径
	
	private Integer specialSubjectID;//关联的专题ID
	
	private SpecialSubject specialSubject;//关联的专题

	@Id
	@GeneratedValue
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "title", length = 100)
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade =  {CascadeType.REMOVE })
	@JoinColumn(name="image_id")
	public ImageInfo getImageInfo() {
		return imageInfo;
	}

	public void setImageInfo(ImageInfo imageInfo) {
		this.imageInfo = imageInfo;
	}

	@Column(name = "uploadtime")
	public Date getUploadTime() {
		return uploadTime;
	}

	public void setUploadTime(Date uploadTime) {
		this.uploadTime = uploadTime;
	}

	@Column(name = "modifytime")
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "auditstate_id")
	public AuditState getAuditState() {
		return auditState;
	}

	public void setAuditState(AuditState auditState) {
		this.auditState = auditState;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="userinfo_id")
	public User getEntryPerson() {
		return entryPerson;
	}

	public void setEntryPerson(User entryPerson) {
		this.entryPerson = entryPerson;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="modify_userinfo_id")
	public User getModifyPerson() {
		return modifyPerson;
	}

	public void setModifyPerson(User modifyPerson) {
		this.modifyPerson = modifyPerson;
	}

	@Column(name = "begintime")
	public Date getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	@Column(name = "endtime")
	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	@Column(name = "link_address")
	public String getLinkAddress() {
		return linkAddress;
	}

	public void setLinkAddress(String linkAddress) {
		this.linkAddress = linkAddress;
	}

	@Column(name = "channel_id",length=10)
	public String getChannelID() {
		return channelID;
	}

	public void setChannelID(String channelID) {
		this.channelID = channelID;
	}

	@Transient
	public String getChannelName() {
		this.channelName = Constants.advertisementMap.get(channelID);
		return channelName;
	}

	public void setChannelName(String channelName) {
		this.channelName = channelName;
	}

	@Transient
	public String getImagepath() {
		if( imageInfo!= null ){
			this.imagepath = Constants.IMAGE_SERVER+Constants.AdvertiseFlag+"/"+imageInfo.getId()+"."+imageInfo.getSuffix();
		}
		else{
			this.imagepath = Constants.IMAGE_SERVER ;
		}
		return imagepath;
	}

	@Column(name = "specialsubject_id")
	public Integer getSpecialSubjectID() {
		return specialSubjectID;
	}

	public void setSpecialSubjectID(Integer specialSubjectID) {
		this.specialSubjectID = specialSubjectID;
	}

	@Transient
	public SpecialSubject getSpecialSubject() {
		SpecialSubjectManager specialSubjectManager = SpringContextHolder.getBean("specialSubjectManager");
		this.specialSubject = specialSubjectManager.get(specialSubjectID);
		return specialSubject;
	}

	public void setSpecialSubject(SpecialSubject specialSubject) {
		this.specialSubject = specialSubject;
	}
	
}
